package com.hrt.datasender.datacollect.drive.modbus.tcp;

import java.util.HashMap;

import com.btcode.common.MyLog;
import com.btcode.log.ILog;
import com.hrt.datasender.datacollect.drive.DriveConfigModel;
import com.hrt.datasender.datacollect.drive.DriveTimer;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.util.ModbusDecodeUtil;

/**
 * modbus数据询问器(modbusTCP协议)
 * 
 * @author wencheng
 *
 */
public class ModbusNetDataAsker extends DriveTimer {
    
    private final int msgLength = 12;
    
    private final int askMsgMBapBit3 = 3;
    
    private final int askMsgMBapBit4 = 4;
    
    private final int askMsgMBapBit5 = 5;
    
    private final int slaveNoBit = 6;
    
    private final int functionCodeBit = 7;
    
    private final int startAddress01Bit = 8;
    
    private final int startAddress02Bit = 9;
    
    private final int dataLength01Bit = 10;
    
    private final int dataLength02Bit = 11;
    
    private final int maskCode = 0x000000FF;
    
    private final int shiftBit8 = 8;
    
    private final int heartBreakInterval = 20;
    
    private final int mbapCode = 0x06;

    private ILog log = null;

    private ModbusTCPSocketClient socket = null;
    private byte[] askMsg = null;

    @Override
    public void executeTimer(DriveConfigModel configs, BcDevicesModel deviceData) {
        try {
            log = MyLog.getInstance().getLogger(getClass().getName() + "." + deviceData.getDeviceName());
            askData(deviceData, configs);
            log.info("询问");
        }
        catch (Exception e) {
            log.error("执行定时任务出错", e);
        }

    }

    private void askData(BcDevicesModel device, DriveConfigModel deviceConfigs) {
        if (socket == null) {
            initSocket(device, deviceConfigs);
        }

        if (socket.isActivite()) {
            socket.writeMsg(askMsg);
        }
    }

    /**
     * 初始化socket
     * 
     * @param device
     * @param deviceConfigs
     * @return
     */
    private void initSocket(BcDevicesModel device, DriveConfigModel deviceConfigs) {
        HashMap<String, String> configs = deviceConfigs.getConfig();
        String ip = configs.get("ip");
        Integer port = Integer.parseInt(configs.get("port"));
        int slaveNo = Integer.parseInt(configs.get("slaveNo"));
        int startNo = Integer.parseInt(configs.get("startNo")) - 1;
        int dataLength = Integer.parseInt(configs.get("dataLength"));
        String driveInstanceId = configs.get("driveInstanceId");
        int functionCode = Integer.parseInt(configs.get("functionCode"));
        socket = new ModbusTCPSocketClient(ip, port, heartBreakInterval, dataLength, driveInstanceId);
        socket.startNetty();

        int startAddress01 = (maskCode & (startNo >> shiftBit8));
        int startAddress02 = (maskCode & startNo); // 如果起始位是255以上，这里就会有问题
        int dataLength01 = (maskCode & (dataLength >> shiftBit8));
        int dataLength02 = (maskCode & dataLength); // 如果超过255，这里就会有问题
        // MBAP报文头 00 00 00 00 00 06
        // modbus TCP协议
        askMsg = new byte[msgLength];        
        askMsg[0] = (byte) 0x00;
        askMsg[1] = (byte) 0x00;
        askMsg[2] = (byte) 0x00;
        askMsg[askMsgMBapBit3] = (byte) 0x00;
        askMsg[askMsgMBapBit4] = (byte) 0x00;
        askMsg[askMsgMBapBit5] = (byte) mbapCode;
        //
        askMsg[slaveNoBit] = (byte) slaveNo;
        askMsg[functionCodeBit] = (byte) functionCode;
        askMsg[startAddress01Bit] = (byte) startAddress01;
        askMsg[startAddress02Bit] = (byte) startAddress02;
        askMsg[dataLength01Bit] = (byte) dataLength01;
        askMsg[dataLength02Bit] = (byte) dataLength02;
        String askMsgStr = ModbusDecodeUtil.byteArrToHex(askMsg);
        System.out.println(askMsgStr);
    }
}
